package com.fjrz.bbxwj.main.host.side

import android.app.Activity
import android.content.Intent
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.fjrz.bbxwj.R
import com.fjrz.bbxwj.entity.FeedbackType
import com.fjrz.bbxwj.mode.vm.FeedbackViewModel
import com.fjrz.bbxwj.popup.BottomPickerPopup
import com.module.lemlin.view.DialogHintPopup
import com.module.lemlin.mode.ViewModeActivity
import com.module.lemlin.rxhttp.Status
import com.module.lemlin.rxhttp.code
import com.module.lemlin.rxhttp.msg
import kotlinx.android.synthetic.main.activity_main_feedback.*

class MainFeedbackActivity : ViewModeActivity<FeedbackViewModel>() {

    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, MainFeedbackActivity::class.java)
            activity.startActivity(intent)
        }
    }


    private var mFeedbackType: FeedbackType? = null

    private val mBottomCatePopup: BottomPickerPopup by lazy {
        BottomPickerPopup(context = this, onConfirm = { action, position ->
            val feedbackType: MutableList<FeedbackType> =
                viewModel.feedbackTypeLiveData.value?.httpData?.data
                    ?: return@BottomPickerPopup
            tvFeedbackType.text = feedbackType[position].name
            mFeedbackType = feedbackType[position]

            tvFeedbackConfirm.isEnabled =
                !etFeedbackContent.text.isNullOrEmpty() && mFeedbackType != null
        })
    }

    private val hintPopup: DialogHintPopup by lazy {
        DialogHintPopup(context = this,
            onConfirm = {
                hintPopup.dismiss()
                finish()
            },
            onCancel = {

            })
    }

    override fun viewModel(): FeedbackViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_main_feedback

    override fun stateBar(): StatusBar {
        return StatusBar(isLightMode = true, view = flFeedbackBar)
    }

    override fun initView() {
        ivFeedbackBack.setOnClickListener { this.finish() }

        tvFeedbackType.setOnClickListener {
                viewModel.feedbackTypeList()

        }

        tvFeedbackConfirm.setOnClickListener {
            val typeId: Int = mFeedbackType?.id
                ?: return@setOnClickListener
            viewModel.feedbackSubmit(typeId, etFeedbackContent.text.toString())
        }

        etFeedbackContent.addTextChangedListener {
            tvFeedbackConfirm.isEnabled = !it.isNullOrEmpty() && mFeedbackType != null
        }

    }

    override fun initViewMode() {
        viewModel.feedbackTypeLiveData.observe(this as LifecycleOwner, Observer { response ->
            if (response.status == Status.FAILURE) {
                toast(response.throwable?.code,response.throwable?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@Observer
            val data: MutableList<String> = mutableListOf()
            response.httpData?.data
                ?.forEach { data.add(it.name) }
            mBottomCatePopup.setFillData(
                title = "请选择反馈类型", pickers = data
            ).showPopupWindow()
        })

        viewModel.feedbackSubmitLiveData.observe(this as LifecycleOwner, Observer { response ->
            if (response.status == Status.FAILURE)
                toast(response.throwable?.code,response.throwable?.msg)
            if (response.status != Status.SUCCESS)
                return@Observer
            val message: String = response.httpData?.msg ?: "提交成功"
            hintPopup.isTextCancelGone(true)
                .setText(
                    textTitle = "提示",
                    textMessage = message,
                    textConfirm = "好的",
                    textCancel = null
                ).setBackPressEnable(false)
                .setOutSideDismiss(false)
                .showPopupWindow()
        })
    }
}